﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlHelper SqlHelper = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGV();
            }
         
        }

        private void BindGV()
        {
            string sql = "select * from StudentInfo";
            GridView1.DataSource = SqlHelper.Get(sql, null);
            GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string sql = "select * from StudentInfo where stu_name like '%'+@name+'%'";
            SqlParameter[] pars = {
                new SqlParameter("@name",name)
            };
            GridView1.DataSource = SqlHelper.Get(sql,pars);
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string name = UserName.Text;
            string pwd = Email.Text;
            string email = Password.Text;
            string intro = Intro.Text;
            string sql = "insert into StudentInfo (stu_name,password,email,intro)values(@name,@email,@pwd,@intro) ";
            SqlParameter[] pars = {
               new SqlParameter("@name",name),
                new SqlParameter("@email",email),
                 new SqlParameter("@pwd",pwd),
                  new SqlParameter("@intro",intro)
            };

            if (SqlHelper.Excute(sql, pars))
            {
                BindGV();
            }
            else
            {
                Response.Write("注册失败！");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGV();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGV();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text;
            string pwd = (GridView1.Rows[e.RowIndex].Cells[1].Controls[1] as TextBox).Text;
            string email = (GridView1.Rows[e.RowIndex].Cells[2].Controls[2] as TextBox).Text;
            string intro = (GridView1.Rows[e.RowIndex].Cells[3].Controls[3] as TextBox).Text;
            string sql = "update StudentInfo set stu_name ='@name',update StudentInfo set password ='@pwd',update StudentInfo set email ='@email',update StudentInfo set intro  ='@intro'";

            SqlParameter[] pars = {
               new SqlParameter("@name",name),
                new SqlParameter("@email",email),
                 new SqlParameter("@pwd",pwd),
                  new SqlParameter("@intro",intro)
            };

            GridView1.DataSource = SqlHelper.Excute(sql, pars);
            GridView1.DataBind();
            Response.Write($"{id}   {name}   {pwd}   {email}    {intro}");
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete from where stu_id=@id ";

            SqlParameter[] pars = {
               new SqlParameter("@id",id),

            };
            GridView1.DataSource = SqlHelper.Excute(sql, pars);
            GridView1.DataBind();
            Response.Write($"{id}  ");
        }
    }
}